/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		Broadcastweb.eu - Paules												/
/	Site:			http://www.broadcastweb.eu												/
/	Email:			support@broadcastweb.eu													/
/	Copyright:		2011 - 2012 (c) Broadcastweb.eu											/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0003																	/
/																							/
/========================================== JooSam ========================================*/

// activate when sam click checkbox view
function samCatViewCheckbox(){
	var target = document.getElementById('jform_view_categories');
	var given = document.getElementsByClassName('samViewMe');
	var data = '';
	var total = 0;
	for(var i = 0; i < given.length; i++ ){
		if(given[i].checked == true){data += given[i].value + ''; total++; }
	}
	var pResult = data.length;
	data = data.slice(0,pResult-1);
	var result = data;
	if(result == ''){ result = 'none'; }
	target.value = result
}

// activate when sam click checkbox Request
function samCatReqCheckbox(){
	var target = document.getElementById('jform_accapt_categories');
	var given = document.getElementsByClassName('samReqMe');
	var data = '';
	var total = 0;
	for(var i = 0; i < given.length; i++ ){
		if(given[i].checked == true){data += given[i].value + ''; total++; }
	}
	var pResult = data.length;
	data = data.slice(0,pResult-1);
	var result = data;
	if(result == ''){ result = 'none'; }
	target.value = result
}

function alreadyDefaultExist(){
	document.getElementById('jform_auto_update2').checked = false;
	document.getElementById('jform_auto_update1').checked = true;
	alert('Only 1 default event per server allowed.');
}

// On document loaded
function onLoadReqevt(){
	
	// Check needed days
	var mon = document.getElementById('jform_evt_day_mon');
	var tue = document.getElementById('jform_evt_day_tue');
	var wed = document.getElementById('jform_evt_day_wed');
	var thu = document.getElementById('jform_evt_day_thu');
	var fri = document.getElementById('jform_evt_day_fri');
	var sat = document.getElementById('jform_evt_day_sat');
	var sun = document.getElementById('jform_evt_day_sun');
	var monT = document.getElementById('jform_evtday0');
	var tueT = document.getElementById('jform_evtday1');
	var wedT = document.getElementById('jform_evtday2');
	var thuT = document.getElementById('jform_evtday3');
	var friT = document.getElementById('jform_evtday4');
	var satT = document.getElementById('jform_evtday5');
	var sunT = document.getElementById('jform_evtday6');
	if(mon.value == '1'){ monT.checked = true; }
	if(tue.value == '1'){ tueT.checked = true; }
	if(wed.value == '1'){ wedT.checked = true; }
	if(thu.value == '1'){ thuT.checked = true; }
	if(fri.value == '1'){ friT.checked = true; }
	if(sat.value == '1'){ satT.checked = true; }
	if(sun.value == '1'){ sunT.checked = true; }

	// check needed category views
	var cv = document.getElementById('jform_view_categories');
	var cvValue = cv.value;
	if(cvValue != 'none'){
		var cvPreArray = cvValue.substring(1, cvValue.length-1);
		var cvArray = new Array();
		cvArray = cvPreArray.split("','");
		for(var i = 0; i < cvArray.length; i++){
			var cvSvalue = cvArray[i];
			var cvCheckT = document.getElementById('samcatview_'+cvSvalue);
			if(cvCheckT != null){ cvCheckT.checked = true; }
		}
	}

	// Check needed category requests
	var cr = document.getElementById('jform_accapt_categories');
	var crValue = cr.value;
	if(crValue != 'none'){
		var crPreArray = crValue.substring(1, crValue.length-1);
		var crArray = new Array();
		crArray = crPreArray.split("','");
		for(var i = 0; i < crArray.length; i++){
			var crSvalue = crArray[i];
			var crCheckT = document.getElementById('samcatrequ_'+crSvalue);
			if(crCheckT != null){ crCheckT.checked = true; }
		}
	}

	// Check for songtypes
	var st = document.getElementById('jform_view_songtype');
	var stValue = st.value;
	if(stValue != ''){
		var stPreArray = stValue.substring(1, stValue.length-1);
		var stArray = new Array();
		stArray = stPreArray.split("','");
		for(var i = 0; i < stArray.length; i++){
			var stSvalue = stArray[i];
			if(stSvalue == 'S'){ document.getElementById('jform_view_songtypes0').checked = true; }
			else if(stSvalue == 'I'){ document.getElementById('jform_view_songtypes1').checked = true; }
			else if(stSvalue == 'P'){ document.getElementById('jform_view_songtypes2').checked = true; }
			else if(stSvalue == 'J'){ document.getElementById('jform_view_songtypes3').checked = true; }
			else if(stSvalue == 'A'){ document.getElementById('jform_view_songtypes4').checked = true; }
			else if(stSvalue == 'N'){ document.getElementById('jform_view_songtypes5').checked = true; }
			else if(stSvalue == 'V'){ document.getElementById('jform_view_songtypes6').checked = true; }
			else if(stSvalue == 'X'){ document.getElementById('jform_view_songtypes7').checked = true; }
			else if(stSvalue == 'C'){ document.getElementById('jform_view_songtypes8').checked = true; }
			else if(stSvalue == '?'){ document.getElementById('jform_view_songtypes9').checked = true; }
		}
	}

	var update = document.getElementById('jform_auto_update2').checked;
	if(update == true){
		document.getElementById('jform_evt_date').setAttribute('style', 'display:none;');
		document.getElementById('jform_evt_date-lbl').setAttribute('style', 'display:none;');
		document.getElementById('jform_evt_date_img').setAttribute('style', 'display:none;');
		document.getElementById('jform_evt_time_start').setAttribute('style', 'display:none;');
		document.getElementById('jform_evt_time_end').setAttribute('style', 'display:none;');
		document.getElementById('jform_evt_time_start-lbl').setAttribute('style', 'display:none;');
		document.getElementById('jform_evt_time_end-lbl').setAttribute('style', 'display:none;');
		document.getElementById('jform_evtday-lbl').setAttribute('style', 'display:none;');
		document.getElementById('jform_evtday').setAttribute('style', 'display:none;');
		document.getElementById('jform_auto_update-lbl').setAttribute('style', 'display:none;');
		document.getElementById('jform_auto_update').setAttribute('style', 'display:none;');
	}
	else{
		document.getElementById('jform_auto_update2').setAttribute('onclick', 'alreadyDefaultExist();');
	}

	// Prepare Textarea
	document.getElementById('jform_more_info_html').setAttribute('onblur', 'jsm_prview();');

	
}

// Preview System
function jsm_prview(){
	var src = document.getElementById('jform_more_info_html').value;
	var tra = document.getElementById('htmlpreview');
	if(src != ''){
		var image = '<img src="../media/com_joosam/images/prev_artist.png" width="100" height="100"/>';
		var reqButton = '<button onclick="return false">Request</button>';
		var buyButton = '<button onclick="return false">Buy</button>';
		var rimg = '<img src="../media/com_joosam/images/stars/star1_blue.png" class="jsm_rankstar" border="0" width="14" height="14" />';
		var rating = rimg+rimg+rimg+rimg;

		// Replace image
		var html1 = src.replace('{jsm_loadimage}', image);
		// Replace Buy
		var html2 = html1.replace('{jsm_loadbuybutton}', buyButton);
		// Replace Request
		var html3 = html2.replace('{jsm_loadrequest}', reqButton);
		// Replace Rating
		var html4 = html3.replace('{jsm_loadrating}', rating);
		// Replace Duration
		var html5 = html4.replace('{jsm_loadduration}', '1:46');
		// Replace Title
		var html6 = html5.replace('{jsm_loadtitle}', 'Songtitle');
		// Replace Artist
		var html7 = html6.replace('{jsm_loadartist}', 'Song Artist');
		// Replace Lyrics
		var html8 = html7.replace('{jsm_loadsongtext}', 'This text will be the Lyrics of this song when you the song info page is loaded');
		// Replace Date Played
		var html9 = html8.replace('{jsm_loadplayed}', '2011-04-22 14:34');
		// Replace Description
		var html10 = html9.replace('{jsm_loaddescr}', 'This text will be the Song info of this song when you the song info page is loaded');
		tra.innerHTML = html10;
	}
}